package com.mybatis.demo.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.transaction.Transaction;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义的一级缓存类
 * @date 2022/11/7 20:05
 */
@Slf4j
public class CachingOneExecutor implements Executor{
    private final Executor delegate;

    public CachingOneExecutor(Executor delegate) {
        log.info("执行一级缓存初始化逻辑");
        this.delegate = delegate;
    }


    public int update(MappedStatement ms, Object parameter) throws SQLException {
        log.info("执行一级缓存[update]");
        return delegate.update(ms, parameter);
    }

    public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
        log.info("执行一级缓存[query]");
        return delegate.query(ms,parameter,rowBounds,resultHandler,cacheKey,boundSql);
    }

    public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
        log.info("执行一级缓存[query]");
        return delegate.query(ms,parameter,rowBounds,resultHandler);
    }

    public <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException {
        log.info("执行一级缓存[queryCursor]");
        return delegate.queryCursor(ms,parameter,rowBounds);
    }

    public List<BatchResult> flushStatements() throws SQLException {
        log.info("执行一级缓存[flushStatements]");
        return delegate.flushStatements();
    }

    public void commit(boolean required) throws SQLException {
        log.info("执行一级缓存[commit]");
        delegate.commit(required);
    }

    public void rollback(boolean required) throws SQLException {
        log.info("执行一级缓存[rollback]");
        delegate.rollback(required);
    }

    public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
        log.info("执行一级缓存[createCacheKey]");
        return delegate.createCacheKey(ms,parameterObject,rowBounds,boundSql);
    }

    public boolean isCached(MappedStatement ms, CacheKey key) {
        log.info("执行一级缓存[isCached]");
        return delegate.isCached(ms,key);
    }

    public void clearLocalCache() {
        log.info("执行一级缓存[clearLocalCache]");
        delegate.clearLocalCache();
    }

    public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType) {
        log.info("执行一级缓存[deferLoad]");
        delegate.deferLoad(ms,resultObject,property,key,targetType);
    }

    public Transaction getTransaction() {
        log.info("执行一级缓存[getTransaction]");
        return delegate.getTransaction();
    }

    public void close(boolean forceRollback) {
        log.info("执行一级缓存[close]");
        delegate.close(forceRollback);
    }

    public boolean isClosed() {
        log.info("执行一级缓存[isClosed]");
        return delegate.isClosed();
    }

    public void setExecutorWrapper(Executor executor) {
    }
}
